package com.lw.vue_shop_server.controller;

import com.lw.vue_shop_server.po.Manager;
import com.lw.vue_shop_server.service.impl.ManagerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ManagerController {

    @Autowired
    public ManagerServiceImpl managerService;
    /**
     * 根据用户名 密码查询用户
     */
    @PostMapping("/login")
    public Map findManagerByNameAndPwd(HttpServletRequest request){
        Map<String,String> user = new HashMap<>();
        user.put("username",request.getParameter("username"));
        user.put("password",request.getParameter("password"));
        Map result = managerService.findManagerByNameAndPwd(user);
        return result;
    }

    @GetMapping("/users")
    public Map getUserlist(HttpServletRequest request){
        Map<String,Object> user = new HashMap<>();
        user.put("query",request.getParameter("query"));
        user.put("pagenum",Integer.parseInt(request.getParameter("pagenum")));
        user.put("pagesize",Integer.parseInt(request.getParameter("pagesize")));
        Map result = managerService.getUserlist(user);
        return result;
    }

    @PostMapping("/users")
    public Map addManager(HttpServletRequest request){
        Manager manager = new Manager();
        manager.setUsername(request.getParameter("username"));
        manager.setPassword(request.getParameter("password"));
        manager.setEmail(request.getParameter("email"));
        manager.setMobile(request.getParameter("mobile"));
        manager.setCreate_time(String.valueOf(System.currentTimeMillis()));
        manager.setMg_state(true);
        return managerService.addManager(manager);
    }

    @GetMapping("/users/{id}")
    public Map findUserById(@PathVariable(value = "id")Integer id){
        Map<String,Object> result = new HashMap<>();
        return managerService.findUserById(id);
    }

    @PutMapping("/users/{id}")
    public Map upadteManager(@PathVariable(value = "id")Integer id,HttpServletRequest request){
        Manager manager = new Manager();
        manager.setId(id);
        manager.setEmail(request.getParameter("email"));
        manager.setMobile(request.getParameter("mobile"));
        return managerService.upadteManager(manager);
    }

    @DeleteMapping("/users/{id}")
    public Map delUserById(@PathVariable(value = "id")Integer id){
        return managerService.delUserById(id);
    }

    @PutMapping("/users/{id}/state/{type}")
    public Map updateStateById(@PathVariable(value = "id")Integer id,@PathVariable(value = "type")boolean type){
        Manager manager = new Manager();
        manager.setId(id);
        manager.setMg_state(type);
        System.out.println(manager);
        return managerService.updateStateById(manager);
    }

    @PutMapping("users/{id}/role")
    public Map updateRoleById(@PathVariable(value = "id")Integer id,HttpServletRequest request){
        Manager manager = new Manager();
        manager.setId(id);
        manager.setRid(Integer.parseInt(request.getParameter("rid")));
        return managerService.updateRoleById(manager);
    }
}
